#include "BlockQueue.hpp"
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include "Task.hpp"
void *producer(void *arg)
{
    int x = 1;
    int y = 1;
    BlockQueue<Task> *bq = static_cast<BlockQueue<Task> *>(arg);
    std::cout << "Producer is producing items..." << std::endl;
    // 生产逻辑省略
    while (true)
    {
        // sleep(1); // 模拟生产耗时
        std::cout << "Produce 任务:" << x << "+" << y << "=?" << std::endl;
        Task data(x, y);
        bq->Equeue(data);

        x++;
        y++;
    }
    return nullptr;
}
void *consumer(void *arg)
{

    BlockQueue<Task> *bq = static_cast<BlockQueue<Task> *>(arg);
    std::cout << "Consumer is consuming items..." << std::endl;
    // 消费逻辑省略
    while (true)
    {
        sleep(1); // 模拟消费耗时
        Task data = bq->Pop();
        data.Execute();
        std::cout << "Consume 任务:" << data.GetX() << "+" << data.GetY() << "=" << data.GetResult() << std::endl;
    }
    return nullptr;
}

int main()
{
    BlockQueue<Task> *bq = new BlockQueue<Task>(); // 申请阻塞队列

    // 构建生产消费
    pthread_t p1, c1;
    pthread_create(&p1, nullptr, producer, (void *)bq);
    pthread_create(&c1, nullptr, consumer, (void *)bq);
    pthread_join(p1, nullptr);
    pthread_join(c1, nullptr);

    return 0;
}